package com.jiahe.yehua.cmn.controller.api;

import com.atguigu.yygh.model.cmn.Dict;
import com.jiahe.yehua.cmn.service.DictService;
import com.jiahe.yehua.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 阿良
 * @version 1.0
 * @date 2021/9/6 18:07
 */
@Api(tags = "前台数据字典接口")
@RestController
@RequestMapping("/api/cmn/dict")
public class ApiDictController {
    @Autowired
    private DictService dictService;

    //根据数据id查询子数据列表
    @ApiOperation(value = "根据数据id查询子数据列表")
    @GetMapping("findChildData/{id}")
    public Result findChildData(@PathVariable Long id) {
        List<Dict> list = dictService.findChlidData(id);
        return Result.ok(list);
    }

    //根据dictcode和value查询
    @GetMapping("getName/{dictCode}/{value}")
    public String getName(@PathVariable String dictCode,
                          @PathVariable String value) {
        String dictName = dictService.getNameByParentDictCodeAndValue(dictCode,value);
        return dictName;
    }

    //根据value查询
    @ApiOperation("")
    @GetMapping("getName/{value}")
    public String getName(@PathVariable String value) {
        String dictName = dictService.getNameByParentDictCodeAndValue("",value);
        return dictName;
    }


    @ApiOperation(value = "根据dictCode获取下级节点")
    @GetMapping(value = "/findByDictCode/{dictCode}")
    public Result<List<Dict>> findByDictCode(
            @ApiParam(name = "dictCode", value = "节点编码", required = true)
            @PathVariable String dictCode) {
        List<Dict> list = dictService.findByDictCode(dictCode);
        return Result.ok(list);
    }





    @ApiOperation(value = "根据ParentId获取下级节点")
    @GetMapping(value = "/findByParentId/{parentId}")
    public Result findByParentId(@PathVariable Long parentId) {
        List<Dict> chlidData = dictService.findChlidData(parentId);
        return Result.ok(chlidData);
    }


}
